import re


import PyQt5
from PyQt5 import QtWidgets
import tkinter as tk
from tkinter import filedialog

from stressPrinter.Model import Globalvariables, model_anke_station, model_setLabelByView
from stressPrinter.View.UI_mainWindow import Ui_MainWindow


class test(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(test, self).__init__()
        self.setupUi(self)

    # cancel按钮的方法
    def cancel(self):
        self.close()

    def reading(self):
        # 获取路径
        root = tk.Tk()
        root.withdraw()
        path = filedialog.askdirectory()
        #Globalvariables.FOLDPATH = path
        Globalvariables.ANEKFOLDPATH = path
        if path != ' ':
            for child in self.centralwidget.children():
                for c in child.children():
                    str = c.objectName()
                    if type(c) == PyQt5.QtWidgets.QCheckBox and str[0] != 'A':
                        # print(type(c.objectName()))
                        c.setEnabled(True)
                    if type(c) == PyQt5.QtWidgets.QDateEdit:
                        c.setEnabled(True)
                    if type(c) == PyQt5.QtWidgets.QComboBox:
                        c.setEnabled(True)
                    if type(c) == PyQt5.QtWidgets.QRadioButton:
                        c.setEnabled(True)
    # '保存'按钮的方法
    def save(self):
        root = tk.Tk()
        root.withdraw()
        path = filedialog.askdirectory()

    # '绘图'按钮的方法
    def plotother(self):
        self.setGlobalV()
        """
            三种选择情况判断：只选泰德、只选安科、两者都选
            异常情况：日期越界、日期不匹配
            两者对比画图方法：
                   X轴长度：泰德数据时间戳的科学计数法*86400*（天数-1）
                      步长：固定值86400（一天的秒数）
        """
        try:
            labelGroup = model_setLabelByView.initLabel()
            #model_presets_station.initData(labelGroup)
            #model_anke_station.initData()
        except ValueError as e:
            print(str(e))

    def getStartDate(self):
        startTime = self.startdateEdit.dateTime()
        endTime = self.enddateEdit.dateTime()
        TimeList = [startTime.toString('yyyy-MM-dd'), endTime.toString('yyyy-MM-dd')]
        Globalvariables.SELECT_DATE = TimeList
    def setGlobalV(self):
        self.getStartDate()
        Globalvariables.ANKESTATION = []
        Globalvariables.STATION = []
        Globalvariables.VIEW_SELECT = []
        Globalvariables.SAMP = 0
        Globalvariables.ANKE = 0
        for child in self.centralwidget.children():
            for c in child.children():
                if type(c) == PyQt5.QtWidgets.QCheckBox:
                    if c.isChecked():
                        if '_' in c.objectName():
                            Globalvariables.STATION.append(c.objectName())
                        if 'label' in c.objectName():
                            result = c.objectName().replace('label', "")
                            Globalvariables.VIEW_SELECT.append(result)
                        if 'copy' in c.objectName():
                            Globalvariables.COPY = 1
                        if re.compile(r'A\d+').search(c.objectName()):
                            Globalvariables.ANKESTATION.append(c.objectName())
                            Globalvariables.ANKE = 1
                elif type(c) == PyQt5.QtWidgets.QRadioButton:
                    if c.isChecked():
                        if 'sampleM' in c.objectName():
                            Globalvariables.SAMP = 1
                        if 'sampleH' in c.objectName():
                            Globalvariables.SAMP = 2


